import { Node } from 'cc';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { SkillModelComp } from './model/SkillModelComp';
import { SkillButtonViewComp } from './view/SkillButtonVIewComp';

/** 技能按钮实例 */
@ecs.register('SkillButton')
export class SkillButton extends ecs.Entity {
  // 数据层
  SkillModel!: SkillModelComp;

  // 视图层
  SkillButtonView!: SkillButtonViewComp;
  init() {
    this.addComponents(SkillModelComp);
  }

  /** 加载技能按钮 */
  load(res: {
    id: number;
    level: number;
    parent: Node;
    size: number;
    absolutePos: { left: number; top: number };
  }) {
    const { id, level, parent, size, absolutePos } = res;
    // 1.设置技能id
    this.SkillModel.id = id;
    // 2.设置技能等级
    this.SkillModel.level = level;
    // 3.设置技能按钮大小
    this.SkillModel.btnSize = size;
    // 4.设置技能按钮绝对位置
    this.SkillModel.absolutePos = absolutePos;

    // 5.加载技能节点
    const node = ViewUtil.createPrefabNode(this.SkillModel.prefabPath);
    const mv = node.addComponent(SkillButtonViewComp);
    this.add(mv);
    node.parent = parent;
  }
}
